/**
 * Created by huangzx on 2015/12/7.
 */
define( ['angular',
        'framework/msg/GillionMsgModule',
        'framework/associate/GillionAssociateModule',
        'framework/dataGrid/DataGridModule',
        'framework/pagination/GillionPaginationModule'],
    function (angular) {
        var app = angular.module('ColConfigModule', ['GillionMsgModule', 'GillionAssociateModule', 'DataGridModule', 'GillionPaginationModule']);
        app.controller('ColConfigController', ['$scope', '$http', 'GillionMsgService', 'GillionMsg', 'Arrays', function ($scope, $http,  GillionMsgService, GillionMsg, Arrays) {
            var data = GillionMsgService.getInputData();
            var initAttrIds;
            $http({
                method: 'GET',
                url: '/grule-manager/decision-tables/getCols/' + data.decisionTable.id
            }).success(function(resp){
                $scope.selectedAttrs = resp.data || [];
                initAttrIds = Arrays.extractToArray($scope.selectedAttrs, 'id');
            });

            angular.element('#showAttrs').hide();
            angular.element(document).on('click', '*', function($event){
                var target = $event.target || $event.srcElement;
                if (target!=angular.element("input[name='attrName']")[0] && $.contains(angular.element('#showAttrs')[0], target) === false) {
                    angular.element('#showAttrs').hide();
                }
            });

            var getModelAttributes = _.debounce(function () {
                $http({
                    method: 'GET',
                    url: '/grule-manager/businessModelAttribute/getModelAttributes',
                    params: {
                        modelId: data.modelId,
                        attrName: $scope.attrName
                    }
                }).success(function(resp){
                    $scope.attrs = resp.data;
                    if ($scope.attrs.length > 0) {
                        angular.element('#showAttrs').show();
                    } else {
                        angular.element('#showAttrs').hide();
                    }
                });
            }, 500);

            angular.element("input[name='attrName']").on('click', function(){
                getModelAttributes();
            });

            $scope.attrNameChange = function(){
                getModelAttributes();
            };

            $scope.selectedAttrs = [];
            $scope.selectAttr = function(attr) {
                if (!Arrays.exists($scope.selectedAttrs, attr)) {
                    $scope.selectedAttrs.push(attr);
                }
                angular.element('#showAttrs').hide();
            };

            $scope.removeAttr = function(index) {
                $scope.selectedAttrs.splice(index, 1);
            };

            function save(){
                var delColIds = [],
                    addColIds = [],
                    selectedIds = Arrays.extractToArray($scope.selectedAttrs, 'id');

                angular.forEach(selectedIds, function(selectedId){
                    if (Arrays.indexOf(initAttrIds, selectedId) == -1) {
                        addColIds.push(selectedId);
                    }
                });
                angular.forEach(initAttrIds, function(initattrId){
                    if (Arrays.indexOf(selectedIds, initattrId) == -1) {
                        delColIds.push(initattrId);
                    }
                });

                $http({
                    method: 'POST',
                    url: '/grule-manager/decision-tables/saveColConfig/' + data.decisionTable.id,
                    data: {
                        delColIds: delColIds,
                        addColIds: addColIds
                    }
                }).success(function(resp){
                    if (resp.success) {
                        GillionMsgService.setOutputData(true);
                        GillionMsgService.close();
                    }
                });
            }

            $scope.confirm = function(){
                if (data.init) {
                    save();
                } else {
                    GillionMsg.confirm('警告', '保存列设置后可能影响目前决策表内容', function(flag){
                        if (flag) {
                            save();
                        }
                    });
                }
            };

            $scope.cancel = function(){
                GillionMsgService.close();
            };

        }]);
        return app;
    }
);